//
//  BaoCheMapViewController.m
//  QMCXDriver
//
//  Created by Massimo on 2017/4/15.
//  Copyright © 2017年 Massimo. All rights reserved.
//

#import "BaoCheMapViewController.h"
#import <QMMapKit.h>
#import "QCustomAnnotationView.h"
#import "LLSlider.h" 
#import "CustomCalloutView.h"
@interface BaoCheMapViewController ()<QMapViewDelegate,QMSSearchDelegate,LLSliderDelegate>

@property (nonatomic, strong) QMSSearcher *searcher;
@property (nonatomic,weak) IBOutlet QMapView *mapView;
@property (nonatomic,weak) IBOutlet UIButton *navButton;
@property (nonatomic,weak) IBOutlet LLSlider *finishSlider;
@end

@implementation BaoCheMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"包车";
    [self.view sendSubviewToBack:self.mapView];
    self.mapView.delegate = self;
    self.mapView.showsUserLocation = YES;
    [self.mapView setUserTrackingMode:QMUserTrackingModeNone];
    [self.mapView setZoomLevel:15];
     CLLocationCoordinate2D from =  CLLocationCoordinate2DMake(22.281968, 114.163177);
    [self.mapView setCenterCoordinate:from animated:YES];
    
    
    self.searcher = [[QMSSearcher alloc] init];
    [self.searcher setDelegate:self];
    [self setupPolyLine];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addAnnotation:(NSString *)title subtitle:(NSString*)subtitle coordinate:(CLLocationCoordinate2D)coordinate{
    QPointAnnotation *annotation = [[QPointAnnotation alloc] init];
    [annotation setTitle:title];
    [annotation setSubtitle:subtitle];
    [annotation setCoordinate:coordinate];
    [annotation setCustomCalloutView:[CustomCalloutView calloutViweWith:annotation.title subtitle:annotation.subTitle]];
    [self.mapView addAnnotations:@[annotation] animated:YES];
}


- (void)setupPolyLine{
   CLLocationCoordinate2D from =  CLLocationCoordinate2DMake(22.281968, 114.163177);
    CLLocationCoordinate2D to =  CLLocationCoordinate2DMake(22.280968, 114.161177);
    QMSDrivingRouteSearchOption *driving = [[QMSDrivingRouteSearchOption alloc] init];
    [driving setFromCoordinate:from];
    [driving setToCoordinate:to];
    //驾车路线规划支持多种规划策略
    [driving setPolicyWithType:QMSDrivingRoutePolicyTypeLeastDistance];
    [self.searcher searchWithDrivingRouteSearchOption:driving];
    
    
    [self addAnnotation:@"FromFromFromFromFromFromFrom" subtitle:@"起点起点起点起点起点起点起点起点起点起点" coordinate:from];
     [self addAnnotation:@"tototototototototototo" subtitle:@"终点终点终点终点终点终点终点终点" coordinate:to];
}


- (void)dealDrivingRoute:(QMSDrivingRouteSearchResult *)drivingRouteResult
{
    QMSRoutePlan *drivingRoutePlan = [[drivingRouteResult routes] firstObject];
    
    NSString *wayPointStr = @"";
    for (int i = 0; i < drivingRoutePlan.waypoints.count; ++i)
    {
        if (i != 0) {
            wayPointStr = [wayPointStr stringByAppendingString:@","];
        }
        QMSRouteWayPoint *wayPoint = [drivingRoutePlan.waypoints objectAtIndex:i];
        wayPointStr = [wayPointStr stringByAppendingString:wayPoint.title];
    }
    
    
    [self.mapView removeOverlays:self.mapView.overlays];
    NSUInteger count = drivingRoutePlan.polyline.count;
    CLLocationCoordinate2D coordinateArray[count];
    for (int i = 0; i < count; ++i)
    {
        [[drivingRoutePlan.polyline objectAtIndex:i] getValue:&coordinateArray[i]];
    }
    
    QPolyline *walkPolyline = [QPolyline polylineWithCoordinates:coordinateArray count:count];
    [self.mapView addOverlay:walkPolyline];
}



#pragma mark - QMSSearchDelegate
- (void)searchWithDrivingRouteSearchOption:(QMSDrivingRouteSearchOption *)drivingRouteSearchOption didRecevieResult:(QMSDrivingRouteSearchResult *)drivingRouteSearchResult{
    
    
    NSLog(@"Driving result:%@", drivingRouteSearchResult);

    
    
    [self dealDrivingRoute:drivingRouteSearchResult];
}


#pragma mark - QMapViewDelegate

- (QOverlayView *)mapView:(QMapView *)mapView viewForOverlay:(id<QOverlay>)overlay
{
    QPolylineView *polylineView = [[QPolylineView alloc] initWithPolyline:overlay];
    
    polylineView.lineWidth = 12/QScreenScale;
    
    polylineView.strokeColor = LLColorRGB(0, 193, 110);
    return polylineView;
}

- (QAnnotationView *)mapView:(QMapView *)mapView viewForAnnotation:(id<QAnnotation>)annotation{
    static NSString *pointAnnotationViewIdentifier = @"customView";
    
     QCustomAnnotationView *annotationView = (QCustomAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:pointAnnotationViewIdentifier];
    if (annotationView == nil) {
        annotationView = [[QCustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointAnnotationViewIdentifier];
        annotationView.imageName = @"icon_map_point";
        annotationView.canShowCallout = YES;
       
    }
    return annotationView;
}

#pragma mark - LLSliderDelegate

- (void)slideEnd:(LLSlider *)slider{
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"结束行程" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否结束行程" preferredStyle:UIAlertControllerStyleActionSheet];
    
    [alertVC addAction:action];
    [alertVC addAction:actionCancel];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
